package com.gmzg.gmzgnews.topic.view;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.gmzg.gmzgnews.R;
import com.gmzg.gmzgnews.topic.adapters.TopicQuestionAdapter;
import com.gmzg.gmzgnews.topic.impl.TopicService;
import com.gmzg.gmzgnews.topic.model.News;
import com.gmzg.gmzgnews.topic.model.NewsParent;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.content.ContentValues.TAG;

/**
 * A simple {@link Fragment} subclass.
 */
public class TopicQuestionFragment extends BaseFragment implements Callback<NewsParent>, SwipeRefreshLayout.OnRefreshListener, TopicQuestionAdapter.onChildClickListener {

    private TopicQuestionAdapter mAdapter;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private TopicService mService;
    private RecyclerView mRecyclerView;
    private ArrayList<News> mNewsList;
    private Call<NewsParent> mCall;

    public TopicQuestionFragment() {
        // Required empty public constructor
    }

    //set fragment's title
    @Override
    public String getFragmentTitle() {
        return "问吧";
    }


    ///////////////////////////////////////////////////////////////////////////
    // lifeCycle Method
    ///////////////////////////////////////////////////////////////////////////
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_topic_question, container, false);
        initView(view);
        retrofitFun();
        recyclerViewFun();
        mSwipeRefreshLayout.setOnRefreshListener(this);

        return view;
    }

    @Override
    public void onPause() {
        if (mCall != null) {
            mCall.cancel();
        }
        super.onPause();
    }

    //RecyclerView
    private void recyclerViewFun() {
        RecyclerView.LayoutManager layoutManager =
                new LinearLayoutManager(
                        getContext(),
                        LinearLayout.VERTICAL,
                        false
                );
        mRecyclerView.setLayoutManager(layoutManager);
        mNewsList = new ArrayList<>();
        mAdapter = new TopicQuestionAdapter(getContext(), mNewsList);
        mAdapter.onChildClickListener(this);
        mRecyclerView.setAdapter(mAdapter);

    }

    public void initView(View view){
        mSwipeRefreshLayout = ((SwipeRefreshLayout) view.findViewById(R.id.topic_fragment_swipe_refresh_layout));
        mRecyclerView = (RecyclerView) view.findViewById(R.id.topic_question_recycler_view);
    }

    //set the refresh drop-down
    @Override
    public void onRefresh() {
        Call<NewsParent> call = mService.getLit("top", 0, 1, 20);
        call.enqueue(this);
        mAdapter.notifyDataSetChanged();
        mSwipeRefreshLayout.setRefreshing(false);
//        Toast.makeText(getContext(), "已经刷新啦", Toast.LENGTH_SHORT).show();
    }


    ///////////////////////////////////////////////////////////////////////////
    // Retrofit Part
    ///////////////////////////////////////////////////////////////////////////
    private void retrofitFun() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.tngou.net")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        mService = retrofit.create(TopicService.class);
        mCall = mService.getLit("top", 0, 1, 20);
        //async---enqueue
        mCall.enqueue(this);
    }

    //successed in request data
    @Override
    public void onResponse(Call<NewsParent> call, Response<NewsParent> response) {
        List<News> newsList = response.body().getNewsList();
        mAdapter.addAll(newsList);
    }

    //failed to request data
    @Override
    public void onFailure(Call<NewsParent> call, Throwable t) {
        if (!mCall.isCanceled()) {
            Toast.makeText(getContext(), "网络出错啦", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onChildClickListener(RecyclerView parent, View view, int position, String urlData) {
        Log.d(TAG, "onChildClickListener: 网址是：" + urlData);
        Intent intent = new Intent(getContext(), TopicDetailActivity.class);
        intent.putExtra("url",urlData);
        startActivity(intent);
    }
}
